/*
 * 金币记录
 * page金币记录
 */
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/bloc/msg/goldRecordBloc.dart';
import 'package:qgchat/common/class/date/index.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/models/goldRecordModel.dart';
import 'package:qgchat/widgets/easyRefreshWidget.dart';
import 'package:qgchat/widgets/noData.dart';
import 'package:qgchat/widgets/avatar.dart';

class Gold extends StatefulWidget {
  Gold({Key key}) : super(key: key);

  @override
  _GoldState createState() => _GoldState();
}

class _GoldState extends State<Gold> {
  String token = '';

  // 生成appbar
  Widget appbarHandle() {
    return PreferredSize(
      preferredSize: Size.fromHeight(px(88)),
      child: AppBar(
        centerTitle: true,
        title: Text(
          "金币",
          style: TextStyle(
            color: Colors.black,
            fontSize: px(34),
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    token = state(context, 'user').info.memberToken;
    return Scaffold(
      appBar: appbarHandle(),
      body: BlocProvider(
        bloc: GoldRecordBloc(token),
        child: BodyHandle(),
      ),
    );
  }
}

class BodyHandle extends StatelessWidget {
  bool isAnchor; // 是否为主播

  // 生成主播列表
  Widget makeAnchorHandle(GoldRecordModel item) {
    return Container(
      height: px(160),
      padding: EdgeInsets.symmetric(horizontal: px(30)),
      child: Row(
        children: <Widget>[
          Avatar(
            item.memberHead,
            true,
            token: item.memberToken,
          ),
          Expanded(
            child: Container(
              padding: EdgeInsets.only(left: px(23)),
              decoration: BoxDecoration(
                border: Border(
                  bottom: BorderSide(
                    color: col("edeff2"),
                    width: 1,
                  ),
                ),
              ),
              child: Column(
                children: <Widget>[
                  // 昵称和时间
                  Container(
                    margin: EdgeInsets.only(top: px(35)),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text(
                          item.memberNickname,
                          style: TextStyle(
                            height: .95,
                            color: col("303133"),
                            fontSize: px(34),
                          ),
                        ),
                        Text(
                          "${Date.simple(int.parse(item.createtime), format: "y/m/d h:i")}",
                          style: TextStyle(
                            height: .99,
                            color: col("bfc2cc"),
                            fontSize: px(26),
                          ),
                        ),
                      ],
                    ),
                  ),
                  // 金币和通话时间
                  Container(
                    margin: EdgeInsets.only(top: px(20)),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Container(
                          child: Row(
                            children: <Widget>[
                              Container(
                                margin: EdgeInsets.only(right: px(10)),
                                child: Image.asset(
                                  "assets/images/icon/star.png",
                                  width: px(32),
                                  height: px(32),
                                ),
                              ),
                              Text(
                                isAnchor
                                    ? (item.type == 0 ? '-' : '+') +
                                        item.gold.toString()
                                    : (item.type == 0 ? '+' : '-') +
                                        item.gold.toString(),
                                style: TextStyle(
                                  height: .99,
                                  color: col("ff7b43"),
                                  fontSize: px(32),
                                ),
                              ),
                            ],
                          ),
                        ),
                        Text(
                          getStyle(item.style),
                          style: TextStyle(
                            color: col("606266"),
                            fontSize: px(26),
                          ),
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  List<Widget> goldList(List<GoldRecordModel> list) {
    return list.map((item) {
      return makeAnchorHandle(item);
    }).toList();
  }

  // 金币收支类型
  String getStyle(int style) {
    switch (style) {
      case 0:
        return '礼物';
        break;
      case 1:
        return '短视频';
        break;
      case 2:
        return '视频通话';
        break;
      case 3:
        return '预约';
        break;
      case 4:
        return '红包';
        break;
      default:
        return '未知收支类型';
    }
  }

  @override
  Widget build(BuildContext context) {
    GoldRecordBloc blog = BlocProvider.of<GoldRecordBloc>(context);
    isAnchor = state(context, 'user').info.memberType == 1;
    return StreamBuilder(
      stream: blog.goldRecordsStream,
      initialData: blog.defaultData,
      builder: (
        BuildContext context,
        AsyncSnapshot snapshot,
      ) {
        List<GoldRecordModel> list = snapshot.data;
        if (!snapshot.hasData) {
          return Center(
            child: CircularProgressIndicator(),
          );
        } else {
          if (list.isEmpty) {
            return NoData("还没有任何我的金币相关消息");
          } else {
            return Container(
              color: Colors.white,
              child: EasyRefresh(
                header: EasyRefreshSetting.header,
                footer: EasyRefreshSetting.footer,
                onRefresh: () async {
                  blog.onRefresh();
                },
                onLoad: () async {
                  blog.getGoldRecordList();
                },
                child: ListView(
                  children: goldList(list),
                ),
              ),
            );
          }
        }
      },
    );
  }
}
